# Docker镜像源修复脚本 - PowerShell版本

Write-Host "🔧 Docker镜像源问题修复工具" -ForegroundColor Green
Write-Host ""

# 检查当前Docker配置
Write-Host "📋 当前Docker信息：" -ForegroundColor Yellow
docker version --format "Docker版本: {{.Client.Version}}"
docker info --format "镜像源: {{.RegistryConfig.IndexConfigs}}"

Write-Host ""
Write-Host "🌐 可用的镜像加速器：" -ForegroundColor Cyan
Write-Host "1. 阿里云：https://docker.mirrors.ustc.edu.cn"
Write-Host "2. 中科大：https://docker.mirrors.ustc.edu.cn" 
Write-Host "3. 网易：https://hub-mirror.c.163.com"
Write-Host "4. 腾讯云：https://mirror.ccs.tencentyun.com"
Write-Host "5. Docker官方（直连）：https://registry-1.docker.io"
Write-Host ""

Write-Host "💡 解决方案：" -ForegroundColor Yellow
Write-Host "方案1: 临时使用官方镜像源构建"
Write-Host "方案2: 配置新的镜像加速器"
Write-Host "方案3: 使用代理构建"
Write-Host ""

$choice = Read-Host "请选择解决方案 (1/2/3)"

switch ($choice) {
    "1" {
        Write-Host "🚀 使用官方镜像源构建..." -ForegroundColor Green
        
        # 临时禁用镜像加速器
        Write-Host "正在构建，这可能需要较长时间..." -ForegroundColor Yellow
        
        # 设置环境变量使用官方源
        $env:DOCKER_BUILDKIT = "0"
        
        try {
            docker-compose -f docker-compose.prod.yml build --no-cache --pull
            Write-Host "✅ 构建成功！" -ForegroundColor Green
        } catch {
            Write-Host "❌ 构建失败，请尝试其他方案" -ForegroundColor Red
        }
    }
    
    "2" {
        Write-Host "🔧 配置镜像加速器..." -ForegroundColor Green
        Write-Host "请手动配置Docker Desktop的镜像加速器：" -ForegroundColor Yellow
        Write-Host "1. 打开 Docker Desktop"
        Write-Host "2. 进入 Settings > Docker Engine"
        Write-Host "3. 添加以下配置："
        Write-Host @"
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.ccs.tencentyun.com"
  ]
}
"@ -ForegroundColor White
        Write-Host "4. 点击 Apply & Restart"
        Write-Host ""
        Write-Host "配置完成后，请重新运行构建命令" -ForegroundColor Cyan
    }
    
    "3" {
        Write-Host "🌐 使用代理构建..." -ForegroundColor Green
        $proxyUrl = Read-Host "请输入代理地址 (格式: http://proxy:port)"
        
        if ($proxyUrl) {
            Write-Host "正在使用代理构建..." -ForegroundColor Yellow
            .\docker-build.ps1 -ProxyUrl $proxyUrl
        } else {
            Write-Host "❌ 代理地址不能为空" -ForegroundColor Red
        }
    }
    
    default {
        Write-Host "❌ 无效选择" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "📞 如果问题仍然存在，请尝试：" -ForegroundColor Cyan
Write-Host "1. 重启 Docker Desktop"
Write-Host "2. 清理 Docker 缓存: docker system prune -a"
Write-Host "3. 检查网络连接"
